<template>
  <el-card>
    <div class="page">
      <h2 class="applyRecode">商家辅助信息</h2>
      <el-form class="form" :model="form" :rules="rules" ref="form" label-width="120px">
        <el-button class="title" type="info" size="mini">店铺客服电话</el-button>
        <el-form-item label="客服电话：" size="small" prop="serviceHotline">
          <el-input placeholder="请输入客服电话" maxlength="50" v-model="form.serviceHotline"></el-input>
        </el-form-item>
        <el-button class="title" type="info" size="mini">本单位发票信息</el-button>
        <el-form-item label="单位名称：" size="small" prop="invCompany">
          <el-input placeholder="单位名称" maxlength="50" v-model="form.invCompany"></el-input>
        </el-form-item>
        <el-form-item label="纳税人识别号：" size="small" prop="invCode">
          <el-input placeholder="纳税人识别号" maxlength="50" v-model="form.invCode"></el-input>
        </el-form-item>
        <el-form-item label="单位地址：" size="small" prop="invRegAddr">
          <el-input placeholder="单位地址" maxlength="255" v-model="form.invRegAddr"></el-input>
        </el-form-item>
        <el-form-item label="单位电话：" size="small" prop="invRegPhone">
          <el-input placeholder="单位电话" maxlength="50" v-model="form.invRegPhone"></el-input>
        </el-form-item>
        <el-form-item label="开户银行：" size="small" prop="invRegBname">
          <el-input placeholder="开户银行" maxlength="10" v-model="form.invRegBname"></el-input>
        </el-form-item>
        <el-form-item label="银行账号：" size="small" prop="invRegBaccount">
          <el-input placeholder="银行账号" maxlength="50" v-model="form.invRegBaccount"></el-input>
        </el-form-item>
      </el-form>
      <div class="btns">
        <el-button type="primary" size="small" @click="cancel">取消</el-button>
        <el-button type="primary" size="small" @click="save('form')">保存</el-button>
      </div>
    </div>
  </el-card>
</template>
<script>
export default {
  name: 'auxiliary',
  data () {
    return {
      supplementId: '',
      form: {
        serviceHotline: '',
        invCompany: '',
        invCode: '',
        invRegAddr: '',
        invRegPhone: '',
        invRegBname: '',
        invRegBaccount: ''
      },
      rules: {
        serviceHotline: [
          { required: true, message: '请输入客服电话', trigger: 'blur' }
          // { pattern: /^\d{3,4}-\d{8}$/ig, message: '电话格式不正确！', trigger: 'blur' }
        ],
        invCompany: [
          { required: true, message: '请输入单位名称', trigger: 'blur' }
        ],
        invCode: [
          { required: true, message: '纳税人识别号', trigger: 'blur' },
          { pattern: /^([0-9]|\d{15}|\d{18}\d{20})$/, message: '纳税人识别号格式不正确！', trigger: 'blur' }
        ],
        invRegAddr: [
          { required: true, message: '单位地址', trigger: 'blur' }
        ],
        invRegPhone: [
          { required: true, message: '请填写单位电话！', trigger: 'blur' },
          { pattern: /^\d{3,4}-\d{8}$/ig, message: '电话格式不正确！', trigger: 'blur' }
        ],
        invRegBname: [
          { required: true, message: '请填写开户银行！', trigger: 'blur' },
          { pattern: /^[\u4e00-\u9fa5]+$/, message: '格式不正确！', trigger: 'blur' }
        ],
        invRegBaccount: [
          { required: true, message: '请输入银行账号', trigger: 'blur' },
          { pattern: /^([0-9]|\d{16}|\d{18,21})$/, message: '银行账号格式不正确！', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.init()
  },
  methods: {
    init () {
      this.$tkAjax.get(`${this.$config.baseUrl}/merchant/merchantSupplement/getMerchantSupplementByMerchantId?merchantId=` + this.$store.state.merchantId).then(res => {
        this.form = res.data.content
        this.supplementId = res.data.content.supplementId
      }).catch(err => {
        this.$message.warning(err.data.message)
      })
    },
    //编辑
    edit () {
      let json = {
        supplementId: this.supplementId,
        serviceHotline: this.form.serviceHotline,
        invCompany: this.form.invCompany,
        invCode: this.form.invCode,
        invRegAddr: this.form.invRegAddr,
        invRegPhone: this.form.invRegPhone,
        invRegBname: this.form.invRegBname,
        invRegBaccount: this.form.invRegBaccount
      }
      this.$tkAjax.post(`${this.$config.baseUrl}/merchant/merchantSupplement/updateMerchantSupplementById`, {}, {
        params: {
          condition: json
        }
      }).then(res => {
        this.$message.success(res.data.message)
      }).catch(err => {
        this.$message.warning(err.data.message)
      })
    },
    save () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.supplementId) {
            this.edit()
          } else {
            let json = {
              merchantId: this.$store.state.merchantId,
              serviceHotline: this.form.serviceHotline,
              invCompany: this.form.invCompany,
              invCode: this.form.invCode,
              invRegAddr: this.form.invRegAddr,
              invRegPhone: this.form.invRegPhone,
              invRegBname: this.form.invRegBname,
              invRegBaccount: this.form.invRegBaccount
            }
            this.$tkAjax.post(`${this.$config.baseUrl}/merchant/merchantSupplement/saveMerchantSupplementByStr`, {}, {
              params: {
                condition: json
              }
            }).then(res => {
              this.supplementId = res.data.content
              this.$message.success(res.data.message)
            }).catch(err => {
              this.$message.warning(err.data.message)
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    cancel () {
      this.$router.push('/')
    }
  }
}
</script>
<style lang="scss" scoped>
.page {
  .title {
    width: 100%;
    border-radius: 20px;
    color: #000;
    background: #fff;
    margin-top: 28px;
  }
  .form {
    width: 80%;
    margin: 0 auto;
    color: #000;
    .el-form-item {
      margin-top: 28px;
      margin-bottom: 0;
      .el-form-item__content {
        margin-left: 32px;
      }
    }
  }
  .btns {
    text-align: center;
    margin: 20px 0 10px;
  }
}
</style>
